home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.11 Nov 91 / Code OOP Sources / BasicCDEFDriver next >
Encoding:
Text File  |  1991-02-09  |  4.6 KB  |  167 lines  |  [TEXT/PJMM]

  1. { THIS CODE REQUIRES Think Pascal v2.0 OR ABOVE TO COMPILE USING THE MULTISEGMENT }
  2. { AND DRIVER OPTIONS }
  3. { Source for a driver that is used as a Control Manager CDEF }
  4. { This CDEF will simulate the standard controls: Button, Checkbox, and Radio button }
  5. { using Object Pascal. }
  6. { It must be called and maintained by a code resource shell that is in turn }
  7. { called and maintained by the Control Manager }
  8. { The driver routines will be called by the CDEF given messages passed to it }
  9. { These messages are translated as follows: }
  10. {  CDEF message    Driver routine }
  11. {  initCntl        Open             Required for Think to set up }
  12. {  dispCntl        Close            Required for Think to close down }
  13. {  all others      Control                Pass message in CodeToDriver^^.fMessage }
  14.  
  15. unit BasicCDEFDriver;
  16.  
  17. interface
  18.  
  19.     uses
  20.         ControlObjUnit;
  21.  
  22.     function Main (theDCE: DCtlPtr; IOPB: ParmBlkPtr; sel: Integer): OSErr;
  23.  
  24. implementation
  25.  
  26.     const
  27. {Define all of the possible driver calls}
  28.         DriverOpen = 0;
  29.         DriverPrime = 1;
  30.         DriverControl = 2;
  31.         DriverStatus = 3;
  32.         DriverClose = 4;
  33.  
  34.     type
  35.         { CodeToDriver passes parameters from the code resource into driver }
  36.         { add or change any parameters that apply to your code resource }
  37.         CodeToDriver = record
  38.                 fMessage: integer;    { message }
  39.                 fVarCode: integer;    { VarCode }
  40.                 fControl: ControlHandle;    { theControl }
  41.                 fParam: LongInt;        { Param }
  42.                 fResult: LongInt;    { result to return to Control Manager }
  43.             end;
  44.         CtoDPtr = ^CodeToDriver;
  45.         CtoDHdl = ^CtoDPtr;
  46.  
  47.     var
  48.     { Think Pascal initializes all globals to 0 before the Open message is called }
  49.         gOpenFlag: integer;    { when 0, we haven't initialized }
  50.  
  51.     { globals and objects }
  52.         gButton: tControlObj;
  53.  
  54. {------------------------------------------------------}
  55.  
  56.     function Main (theDCE: DCtlPtr; IOPB: ParmBlkPtr; sel: Integer): OSErr;
  57.  
  58.     {********************************************}
  59.  
  60.         function OpenRtn: OSErr;
  61.         { create and initialize the object given the varcode }
  62.             var
  63.                 theCtoDHdl: CtoDHdl;
  64.         begin
  65.             OpenRtn := noErr;    { init }
  66.  
  67.             if gOpenFlag = 0 then {DA not already opened}
  68.                 begin
  69.                     theCtoDHdl := CtoDHdl(theDCE^.dCtlPosition);
  70.  
  71.                     { create and initialize the main object based on VarCode }
  72.                     gButton := nil;
  73.                     case theCtoDHdl^^.fVarCode of
  74.                         pushButProc:    { simple button }
  75.                             New(tPushButObj(gButton));
  76.                         checkBoxProc:    { check box }
  77.                             New(tCheckBoxObj(gButton));
  78.                         radioButProc:    { radio button }
  79.                             New(tRadioButObj(gButton));
  80.                         otherwise
  81.                     end;    { case theCtoDHdl^^.fVarCode }
  82.  
  83.                     if gButton <> nil then
  84.                         begin
  85.                             gButton.mInit(theCtoDHdl^^.fControl);    { call the initialize routine }
  86.                             gOpenFlag := 1;    { indicates we're OK for Control calls }
  87.                         end
  88.                     else
  89.                         OpenRtn := -1;    { error! }
  90.                 end;
  91.         end;        { OpenRtn }
  92.  
  93.     {********************************************}
  94.  
  95.         function CloseRtn: OSErr;
  96.         { Free the object }
  97.  
  98.         begin { Close }
  99.             CloseRtn := noErr;    { init }
  100.             if gOpenFlag <> 0 then
  101.                 begin
  102.                     gButton.mFree;
  103.                 end;
  104.         end;        { CloseRtn }
  105.  
  106.     {********************************************}
  107.  
  108.         function ControlRtn: OSErr;
  109.         { processes a code resource message }
  110.             const
  111.                 calcCntlRgn = 10;
  112.             var
  113.                 theCtoDHdl: CtoDHdl;
  114.         begin
  115.             theCtoDHdl := CtoDHdl(theDCE^.dCtlPosition);
  116.  
  117.             with theCtoDHdl^^ do    { theCtoDHdl is locked by caller }
  118.                 case fMessage of
  119.                     drawCntl: 
  120.                         gButton.mDraw(fParam);    { fParam contains the part code }
  121.                     testCntl: 
  122.                         fResult := gButton.mTest(fParam);    { fParam is a mouse point }
  123.                     calcCRgns, calcCntlRgn: 
  124.                         gButton.mCalcRgn(fParam, fMessage);    { fParam is a region handle }
  125.                     posCntl: 
  126.                         gButton.mPosition(fParam);    { fParam is position offset }
  127.                     thumbCntl: 
  128.                         gButton.mCalcThumb(fParam);    { fParam is a pointer to a record }
  129.                     dragCntl: 
  130.                         fResult := gButton.mDrag(fParam);    { fParam indicates the type of drag }
  131.                     autoTrack: 
  132.                         gButton.mAutoTrack(fParam);    { fParam contains the part code }
  133.                     otherwise
  134.                 end;    { case fMessage }
  135.  
  136.         end;        { ControlRtn }
  137.  
  138.     {********************************************}
  139.  
  140.     begin
  141.     { Think Pascal allocates dCtlStorate to its A4 world on as it }
  142.     { processes the Open message.  If it cannot be allocated, test here and fail }
  143.         if theDCE^.dCtlStorage = nil then { A4 word isn't there }
  144.             begin
  145.                 SysBeep(1);    {*** need an alert here ***}
  146.                 Main := -1;
  147.                 Exit(Main);
  148.             end;
  149.  
  150.         case sel of
  151.             DriverOpen: 
  152.                 Main := OpenRtn;
  153.             DriverPrime: 
  154.                 Main := noErr;
  155.             DriverControl: 
  156.                 if gOpenFlag <> 0 then
  157.                     main := ControlRtn;
  158.             DriverStatus: 
  159.                 Main := noErr;
  160.             DriverClose: 
  161.                 Main := CloseRtn;
  162.         end;
  163.     end;        { Main }
  164.  
  165. {------------------------------------------------------}
  166.  
  167. end.    { unit BasicCDEFDriver }